﻿/**
  KCAKCAK   CAKCAKC   AK      CA
 AK        KC     CA  AK    AK
CA         KC     CA  AK  KC
CA         KCAKCAKCA  AKCA
CA         KC     CA  AK  KC
 AK        KC     CA  AK    AK
  KCAKCAK  KC     CA  AK      CA

_________________________________________________________________
 China ataca Kamchatka - copyright 2007(c)Ruben Sainz de la Maza
 
	@class: 	Cliente
	@vertion: 	0.1
	@date:		25/02/08
				
	@language: 	ActionScript 3.0
	@author:	Ruben Sainz de la Maza
			
**/

try { var dummy = Cliente; } catch(e)
{

	/**
	 * cPing
	 * 
	 * @usage   hacer ping contra el servidor
	 * @return  ping
	 */
	Client.prototype.cPing = function()
	{ 
		return; 
	}


	/**
	 * getUsers
	 * 
	 * @usage  devuelve un array de los usuarios conectados 
	 * @return  array
	 */
	Client.prototype.getUsers = function(){
		var arrDatos = new Array();
		for(var i in application.objUsers) arrDatos.push(application.objUsers[i])
		return arrDatos;
	}


	/**
	 * verifyConnectedClient
	 * 
	 * @usage   verifica si existe algun usuario en el server que no es verdadero
	 */
	verifyConnectedClient = function()
	{
	//	trace("#Cliente# verifyConnectedClient");
		var i;
		var l = application.clients.length;
		for(i = 0; i < l; i ++){
			var stillThere = application.clients[i].ping();
			if(!stillThere)application.disconnect(application.clients[i]);
		}
	}


	// Bandwith, UP, DOWN
	Client.prototype.checkBandwidth = function()
	{
	//	trace("#Cliente# checkBandwidth");
		var Cstats = this.getStats();
		var now = (new Date()).getTime();
		var elapsedTime = now - this.lastTime;
		var deltaBytesOut = Cstats.bytes_out - this.lastBytesOut;
		var deltaBytesIn  = Cstats.bytes_in  - this.lastBytesIn;

		var dwS = (deltaBytesOut/elapsedTime) * 1000;
		var upS = (deltaBytesIn/elapsedTime)  * 1000;

		this.lastBytesOut = Cstats.bytes_out;
		this.lastBytesIn  = Cstats.bytes_in;
		this.lastTime = now;

		Astats = application.getStats();

		return({dwS:dwS, upS:upS, bytes_in:Cstats.bytes_in, bytes_out:Cstats.bytes_out, bytes_in:Astats.bytes_in, bytes_out:Astats.bytes_out});
	}
	
	
	/**
	 * redireccionar las llamadas de rooms a la clase Rooms
	 * ej desde cliente: _objProxyClient.serverCall("Rooms", _responder, "popUserLobby", "nick");
	 * ej desde server: application.rooms.pushUserLobby(null, "nick");
	 */
	Client.prototype.Rooms = function(p_method)
	{
		var _method = p_method
		var _rooms	= application.rooms;
		if(_rooms[_method] == undefined){
	//		trace("#application# ERROR: this.rooms" + _method + " no se encuentra el metodo");
			return false;
		} else {
	//		trace("#application# call Rooms " + _method)
			var params = new Array();
			params.push(this);
			var l = arguments.length;
			for(var i = 1; i < l; i++) params[i] = arguments[i];
			var res = _rooms[_method].apply(_rooms, params);
			return res;
		}
	}
	
	
	/**
	 * redireccionar las llamadas de chat a la clase Chat
	 * ej desde cliente: _objProxyClient.serverCall("Chat", _responder, "sendMessage");
	 * ej desde server: application.chat.sendMessage(null, "message");
	 */
	Client.prototype.Chat = function(p_method)
	{
		var _method = p_method
		var _chat	= application.chat;
		if(_chat[_method] == undefined){
	//		trace("#application# ERROR: this.chat" + _method + " no se encuentra el metodo");
			return false;
		} else {
	//		trace("#application# call Chat " + _method)
			var params = new Array();
			params.push(this);
			var l = arguments.length;
			for(var i = 1; i < l; i++) params[i] = arguments[i];
			var res = _chat[_method].apply(_chat, params);
			return res;
		}
	}	
}